package luanvan.app.foodnote.library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import luanvan.app.foodnote.EditFoodActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class FoodFunctions {

	// Progress Dialog
    private ProgressDialog pDialog;
    
    private static final String TAG_ID = "id";
    private static final String TAG_UID = "unique_id";
    private static final String TAG_SUCCESS = "success";
    
    // JSON parser class
    JSONParser jsonParser = new JSONParser();
    
    private static final String url_delete_food = "http://192.168.0.102/foodnoteserver/delete_food.php";
    
	private Context _mContext;
	private String fid;
	private String uid;
	
	public FoodFunctions(Context context, String fid, String uid){
		this._mContext = context;
		this.fid = fid;
		this.uid = uid;
	}
	
	public class DeleteFood extends AsyncTask<Void, String, Void>{

		private int success ;
		
		@Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(_mContext);
            pDialog.setTitle("Xin chờ");
            pDialog.setMessage("Đang xóa ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
		@Override
		protected Void doInBackground(Void... agrs) {
			// TODO Auto-generated method stub
			//String message;
			try {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(TAG_ID, fid));
				params.add(new BasicNameValuePair(TAG_UID, uid));
				JSONObject json = jsonParser.makeHttpRequest(url_delete_food, "POST", params);
				
				// check your log for json response
                Log.d("Delete Food", json.toString());
                success = json.getInt(TAG_SUCCESS);
                if(success == 1){
                	Log.i("Delete Food", "Đã xóa");
                }
			} catch (JSONException e) {
                e.printStackTrace();
            }
			return null;
		}

		protected void onPostExecute(String file_url) {
			pDialog.dismiss();
		}
	}
}
